package com.cff.rest;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cff.dao.ScourtDao;
import com.cff.dao.ScourtInfoDao;
import com.cff.domain.Scourt;
import com.cff.domain.ScourtInfo;
import com.cff.dto.RetCode;
import com.cff.util.DateUtil;

@RestController
@RequestMapping("/api")
public class ApiRest {
	private final static Logger logger = LoggerFactory.getLogger(ApiRest.class);

	@Autowired
	ScourtDao scourtDao;

	@Autowired
	ScourtInfoDao scourtInfoDao;

	@RequestMapping(value = "/book", method = RequestMethod.POST, produces = "application/json")
	public RetCode book(@RequestBody Scourt scourt) {
		RetCode retCode = new RetCode();
		if (scourt == null) {
			retCode.setErrorCode("400");
			retCode.setErrorMsg("数据为空！");
			return retCode;
		}
		try {
			String startTime = scourt.getStartTime();
			String endTime = scourt.getEndTime();
			Date startDate = DateUtil.parse(startTime, DateUtil.FullDateHourPattern);
			Date endDate = DateUtil.parse(endTime, DateUtil.FullDateHourPattern);
			if (!DateUtil.isSameDay(startDate, endDate)) {
				retCode.setErrorCode("300");
				retCode.setErrorMsg("只能是同一天的预订！");
				return retCode;
			}

			String courtName = scourt.getCourtName();
			int courtId = Integer.parseInt(scourt.getCourtId());
			ScourtInfo scourtInfo = scourtInfoDao.findByCourtIdAndCourtName(courtId, courtName);
			if (scourtInfo == null) {
				retCode.setErrorCode("600");
				retCode.setErrorMsg("该场地不存在！");
				return retCode;
			}

			scourt.setCreateTime(DateUtil.format(new Date(), DateUtil.FullDatePatternNoSymbol));
			scourt.setBdate(DateUtil.format(startDate, DateUtil.SimpleDatePattern));
			scourt.setStartHour(startDate.getHours());
			scourt.setEndHour(endDate.getHours());

			List<Scourt> tmp = scourtDao.queryExistOrNot(scourt.getStartHour(), scourt.getEndHour());
			if (tmp != null && tmp.size() > 0) {
				for (Scourt item : tmp) {
					if (item.getCourtId().equals(scourt.getCourtId()) && item.getBdate().equals(scourt.getBdate())) {
						retCode.setErrorCode("700");
						retCode.setErrorMsg(
								item.getBdate() + "日" + item.getStartHour() + "点至" + item.getEndHour() + "点已经被预约！");
						return retCode;
					}
				}
			}

			String userName = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
			scourt.setUserName(userName);
			scourtDao.save(scourt);
			retCode.setErrorCode("200");
			retCode.setErrorMsg("预订成功！");
		} catch (Exception e) {
			e.printStackTrace();
			retCode.setErrorCode("500");
			retCode.setErrorMsg("预订失败！");
		}

		return retCode;
	}

	@RequestMapping(value = "/bookList", method = RequestMethod.POST, produces = "application/json")
	public List<Scourt> bookList(@RequestParam String bookTime) {
		String userName = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
		List<Scourt> list = null;
		if (bookTime == null || bookTime.trim().length() < 1) {
			list = scourtDao.findByUserName(userName);
		} else {
			list = scourtDao.findByUserNameAndBdate(userName, bookTime);
		}

		for (Scourt scourt : list) {
			scourt.setStartTime(scourt.getBdate() + " " + scourt.getStartHour());
			scourt.setEndTime(scourt.getBdate() + " " + scourt.getEndHour());
		}

		return list;
	}

	@RequestMapping(value = "/courtList", method = RequestMethod.POST, produces = "application/json")
	public List<ScourtInfo> courtList() {
		List<ScourtInfo> list = (List<ScourtInfo>) scourtInfoDao.findAll();
		return list;
	}

	@RequestMapping(value = "/getCourtId", method = RequestMethod.POST)
	public int getCourtId(@RequestParam String courtName) {
		ScourtInfo scourtInfo = scourtInfoDao.findByCourtName(courtName);
		return scourtInfo.getCourtId();
	}
}
